package com.xiaoluyy.demo.common.util;

import org.springframework.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class FileUtil {

    public static byte[] readFileByte(String filePath) throws Exception{
        InputStream is = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            is = new FileInputStream(filePath);// pathStr 文件路径
            byte[] b = new byte[1024];
            int n;
            while ((n = is.read(b)) != -1) {
                out.write(b, 0, n);
            }// end while
        } catch (Exception e) {
            throw new Exception("System error,SendTimingMms.getBytesFromFile", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }// end try
            }// end if
        }// end try

        return out.toByteArray();
    }

    /**
     * return the ext of the file
     *
     * @param filename
     * @return
     */
    public static String getExt(String filename) {
        return StringUtils.isEmpty(filename) ? "" : filename.substring(filename
                .lastIndexOf("."));
    }

}
